package practice;

/**
 * 抢红包案例
 * 100个红包，分成3份
 * 5个人来抢
 */
public class Practice4 {
    public static void main(String[] args) {
        People people1 = new People("A");
        People people2 = new People("B");
        People people3 = new People("C");
        People people4 = new People("D");
        People people5 = new People("E");

        people1.start();
        people2.start();
        people3.start();
        people4.start();
        people5.start();


    }

    private static class People extends Thread{

        private static Double money = 100.0;
        private static final Double MIN = 0.01;
        private static int count = 3;

        public People(String name) {
            super(name);
        }

        @Override
        public void run() {
            synchronized (People.class){
                if (money == 0){
                    System.out.println(getName() +  "没有抢到红包");
                }
                else{
                    Double price;
                    if (count == 1){
                        price = money;
                    }
                    else{
                        Double max = money - (count - 1) * MIN;
                        Double randomNum = Math.round(Math.random() * max * 100.0) / 100.0;
                        if (randomNum < MIN){
                            randomNum = MIN;
                        }
                        price = randomNum;

                    }
                    money -= price;
                    count --;
                    System.out.println(getName() + "抢到了" + price + "元");
                }
            }
        }
    }


}
